package cn.dida.core;

import java.lang.reflect.Method;

import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.MethodBeforeAdvice;

/**
 * 主从数据源自动切换拦截器
 * 
 * 切换规则：当dao方法名为select***、getUsers***、getUsers***格式时，切换为从库，否则切换为主库
 * 
 * @author weiwei
 * 
 */
public class MasterSlaveDatasourceAutoChanger implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		String methodName = method.getName();
		if (StringUtils.startsWithAny(methodName, new String[] { "select", "get", "query" })) {
			DatasourceHolder.setDatasourceKey("ds_slave1");
		} else {
			DatasourceHolder.setDatasourceKey("ds_master1");
		}
	}

}
